package org.neuedu.fly.web;


import org.neuedu.fly.entity.User;
import org.neuedu.fly.entity.vo.ResponseEntity;
import org.neuedu.fly.service.UserService;
import org.neuedu.fly.util.GsonUtil;
import org.neuedu.fly.util.SendSmsUtil;
import org.neuedu.fly.util.SysUtil;

import javax.servlet.annotation.WebServlet;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    private UserService userService = new UserService();

    public ResponseEntity userPage() {
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        return userService.page(page, limit);
    }

    public ResponseEntity userCenter() {
        int userId = Integer.parseInt(request.getParameter("userId"));
        return userService.findUser(userId);
    }

    //验证码校验
    public ResponseEntity checkCodeImg() {
        //获取后端存储的验证码码
        String codeImg = (String) session.getAttribute(SysUtil.VERIFY_CODE);
        //获取前端输入的验证码
        String verifyCode = request.getParameter("verifyCode");
        if (!codeImg.equalsIgnoreCase(verifyCode)) {
            return ResponseEntity.error("验证码输入有误！");
        }
        return ResponseEntity.ok();
    }

    //发短信
    public ResponseEntity sendSms() {
        //发送到手机的随机验证码
        String code = String.valueOf(new Double(Math.random() * 10000).intValue());
        System.out.println("手机验证码：" + code);
        session.setAttribute(SysUtil.TEL_CODE, code);
        //发送
        String tel = request.getParameter("tel");
        String smsResult = SendSmsUtil.sendSms(tel, code);
        System.out.println(smsResult);
        //{"Message":"模板不合法(不存在或被拉黑)","RequestId":"10DDECF9-FFF6-4DFB-888A-1EF44FA6FC28","Code":"isv.SMS_TEMPLATE_ILLEGAL"}
        Map<String, String> map = GsonUtil.fromJson(smsResult, Map.class);
        String message = map.get("Message");
        if (!message.equals("OK")) {
            return ResponseEntity.error("发送短信失败！");
        }
        return ResponseEntity.ok("发送短信成功！");
    }

    //登录
    public ResponseEntity login() {
        String tel = request.getParameter("tel");
        String pwd = request.getParameter("pwd");
        ResponseEntity loginEntity = userService.login(tel, pwd);
        //如果成功，则需要把对应的数据存放到session中
        if (ResponseEntity.isSuccess(loginEntity)) {
            session.setAttribute(SysUtil.CURRENT_USER, loginEntity.getData());
            return ResponseEntity.ok();
        }
        return loginEntity;
    }

    //注册
    public ResponseEntity register() {
        //短信校验
        String telCode = request.getParameter("telCode");
        Object sessionTelCode = session.getAttribute(SysUtil.TEL_CODE);
        if (!sessionTelCode.equals(telCode)) {
            return ResponseEntity.error("短信验证码校验失败！");
        }

        //手机号，昵称，密码
        String tel = request.getParameter("tel");
        //校验手机号是否已经注册过了
        ResponseEntity checkedTel = userService.checkedTel(tel);
        if (!ResponseEntity.isSuccess(checkedTel)) {
            return checkedTel;
        }

        //开始添加到数据库中
        String nickName = request.getParameter("nickName");
        String pwd = request.getParameter("pwd");
        User user = User.builder().tel(tel).nickName(nickName).password(pwd).build();
        return userService.register(user);
    }

    //用户退出
    public ResponseEntity logOut() {
        session.invalidate();
        //session.removeAttribute(SysUtil.CURRENT_USER);
        return ResponseEntity.ok("退出成功！");
    }

    public ResponseEntity edit() {
        int uId = Integer.parseInt(request.getParameter("uId"));
        String nickName = request.getParameter("nickName");
        String tel = request.getParameter("tel");
        String email = request.getParameter("email");
        String sex = request.getParameter("sex");
        String city = request.getParameter("city");
        String signature = request.getParameter("signature");
        User newUser = User.builder().id(uId).nickName(nickName).tel(tel).email(email).sex(sex).city(city).signature(signature).build();
        return userService.update(newUser,session);
    }

    public ResponseEntity editPwd() {
        int uId = Integer.parseInt(request.getParameter("uId"));
        String tel = request.getParameter("tel");
        String password = request.getParameter("pass");
        String nowpass = request.getParameter("nowpass");
        return userService.editPwd(uId, tel, password, nowpass);
    }

    public ResponseEntity editHeadImg() {
        return userService.editHeadImg(request);
    }

    public ResponseEntity userStatus(){
        int userId = Integer.parseInt(request.getParameter("userId"));
        int userStatus = Integer.parseInt(request.getParameter("userStatus"));
        return userService.userStatus(userId,userStatus);
    }
}
